import {fetch, post} from '../../../utils/axios'

const state = {
  name: 'SaltMinion列表',
  search: '',
  totalItems: 0,
  items: [],
  opration: '',
  headers: [
    { text: 'Minion', value: 'minion', align: 'center' },
    { text: 'Status', value: 'status', align: 'center' },
    { text: 'Actions', value: 'actions', align: 'center' }
  ],
  etext: 'No Data',
  rowsPerPageItems: [20, 50, 100, { text: 'All', value: 100000 }],
  searchLabel: 'name',
  editedIndex: -1
}

const mutations = {
  loadMinions (state) {
    fetch('/salt/minions_keys/', {
    }).then((response) => {
      let res = JSON.parse(response)
      state.items = res.rows
      state.totalItems = res.total
      state.loading = false
    }).catch((error) => {
      state.etext = error.response
    })
  },
  oprateMinion (state, payload) {
    post('/salt/oprate_keys/', {
      opration: state.opration,
      minion: payload
    }).then((response) => {
      this.commit('minions/loadMinions')
    }).catch((error) => {
      alert(error.response.data)
      this.commit('minions/loadMinions')
    })
  }
}

const actions = {
  loadMinions ({commit}) {
    commit('loadMinions')
  },
  acceptMinion ({commit}, payload) {
    state.opration = 'accept'
    commit('oprateMinion', payload)
  },
  rejectMinion ({commit}, payload) {
    state.opration = 'reject'
    commit('oprateMinion', payload)
  },
  deleteMinion ({commit}, payload) {
    let r = confirm('确认删除？')
    if (r === true) {
      state.opration = 'delete'
      commit('oprateMinion', payload)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
